package com.github.tetrisgame.ui.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;

import com.github.tetrisgame.base.BaseActivity;
import com.github.tetrisgame.databinding.ActivityAdvBinding;
import com.github.tetrisgame.utils.UIUtils;

public class AdvActivity extends BaseActivity<ActivityAdvBinding> {
    AnimatorSet animatorSet1 = new AnimatorSet();
    AnimatorSet animatorSet2 = new AnimatorSet();
    AnimatorSet animatorSet3 = new AnimatorSet();
    private boolean isStart = false;

    @Override
    public ActivityAdvBinding initBinding() {
        return ActivityAdvBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initUI() {
        animation1();
        animation2();
        mBinding.main.setOnClickListener(v -> {
            animatorSet3.start();
            isStart = !isStart;
            if (isStart) {
                animatorSet1.start();
                animatorSet1.resume();
            } else {
                animatorSet1.pause();
            }
        });
    }

    public void animation1() {
        ObjectAnimator animator1 = ObjectAnimator.ofInt(mBinding.animationView, "angel1", 40, -1);
        animator1.setDuration(1000);
        animator1.setRepeatCount(-1);

        ObjectAnimator animator2 = ObjectAnimator.ofInt(mBinding.animationView, "angel2", -40, 1);
        animator2.setDuration(1000);
        animator2.setRepeatCount(-1);

        ObjectAnimator animator3 = ObjectAnimator.ofInt(mBinding.animationView, "centerX", 10 * UIUtils.dp2px(30), 2 * UIUtils.dp2px(30));
        animator3.setRepeatCount(-1);
        animator3.setDuration(1000);

        animatorSet1.playTogether(animator1, animator2, animator3);
    }

    public void animation2() {
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(mBinding.animView, "centerY", UIUtils.dp2px(8), UIUtils.dp2px(102));
        objectAnimator1.setDuration(500);


        ObjectAnimator objectAnimator2 = ObjectAnimator.ofInt(mBinding.animView, "angel1", 0, 180, 0);
        objectAnimator2.setDuration(1000);


        ObjectAnimator objectAnimator3 = ObjectAnimator.ofInt(mBinding.animView, "angel2", 0, -180, 0);
        objectAnimator3.setDuration(1000);

        ObjectAnimator objectAnimator4 = ObjectAnimator.ofInt(mBinding.animView, "num", 15, 105);
        objectAnimator4.setDuration(500);
        objectAnimator4.setStartDelay(500);

        animatorSet2.playTogether(objectAnimator2, objectAnimator3, objectAnimator4);
        animatorSet3.playSequentially(objectAnimator1, animatorSet2);
    }
}